package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.common.R;
import com.itheima.ydd.dto.PageInfo;
import com.itheima.ydd.mapper.YddLinkMapper;
import com.itheima.ydd.pojo.YddCate;
import com.itheima.ydd.pojo.YddLink;
import com.itheima.ydd.service.YddLinkService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class YddLinkServiceImpl extends ServiceImpl<YddLinkMapper, YddLink> implements YddLinkService {

    @Override
    public ResponseEntity<R<PageInfo<YddLink>>> findByPage(String type, String page, String limit) {
        IPage<YddLink> pg = new Page<YddLink>(Integer.parseInt(page),Integer.parseInt(limit));
        LambdaQueryWrapper<YddLink> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(type != null, YddLink::getType ,type);
        this.page(pg,wrapper);

        PageInfo<YddLink> pageInfo = new PageInfo<YddLink>();

        Long total = pg.getTotal();
        Long pages = pg.getPages();
        Long size = pg.getSize();
        Long current = pg.getCurrent();
        List<YddLink> records = pg.getRecords();

        pageInfo.setCount(total.intValue());
        pageInfo.setTotalPages(pages.intValue());
        pageInfo.setPageSize(size.intValue());
        pageInfo.setCurrentPage(current.intValue());
        pageInfo.setData(records);

        return ResponseEntity.ok(R.success(pageInfo));
    }

    @Override
    public ResponseEntity<R<String>> add(YddLink yddLink) {
        this.save(yddLink);
        return ResponseEntity.ok(R.success("添加友情链接成功"));
    }

    @Override
    public ResponseEntity<R<String>> del(Integer id) {
        this.removeById(id);
        return ResponseEntity.ok(R.success("删除友情链接成功!"));
    }

    @Override
    public ResponseEntity<R<String>> updateLink(YddLink yddLink) {
        this.updateById(yddLink);
        return ResponseEntity.ok(R.success("更新友情链接成功!"));
    }

    @Override
    public ResponseEntity<R<YddLink>> rowInfo(Integer id) {
        YddLink data = this.getById(id);
        return ResponseEntity.ok(R.success(data));
    }
}
